import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:hxdj/common/my_header_global.dart';
enum AlertType {
  iosMode, //ios的风格的弹框
  sysTheme, //系统主题的弹框
  customWidget, //自定义内容
  sheetPayStatue, //支付状态弹框
}

class MyAlertManager {
// 封装成一个函数
  static Future<void> showAlert(AlertType alertType, BuildContext context, String title, String content,
      {VoidCallback? onClose,
      VoidCallback? onSure,
      Widget? child,
      String? close,
      String? sure,
      Map<String, dynamic>? object}) async {
    return showDialog(
        context: context,
        builder: (BuildContext context) {
          switch (alertType) {
            case AlertType.iosMode:
              return _buildIOSModeWidgte(context, title, content,
                  onClose: onClose, onSure: onSure, close: close, sure: sure);
            case AlertType.sysTheme:
              return _buildSysModeWidgte(context, title, content, onClose: onClose, onSure: onSure);
            case AlertType.customWidget:
              return _buildCustomWidget(child!, onClose: onClose, onSure: onSure);
            default:
              return _buildSysModeWidgte(context, title, content, onClose: onClose, onSure: onSure);
          }
        });
  }

  static Future<void> sheetAlert(AlertType alertType, BuildContext context, String title, String content,
      {VoidCallback? onClose,
      VoidCallback? onSure,
      Map<String, dynamic>? object,
      Widget? child,
      Function(dynamic)? parmaCallback}) async {
    return showModalBottomSheet(
        backgroundColor: Colors.transparent,
        context: context,
        isScrollControlled: true,
        builder: (aContext) {
          switch (alertType) {
            case AlertType.sheetPayStatue:
              return _buildSheetPayStatue(context, title, content, onClose: onClose, onSure: onSure, object: object);

            case AlertType.customWidget:
              return _buildSheetCustomWidget(child!);

            default:

          }
          return _buildSheetPayStatue(context, title, content, onClose: onClose, onSure: onSure, object: object);
        });
  }

  //系统主题的弹框
  static _buildSysModeWidgte(BuildContext context, String title, String content,
      {VoidCallback? onClose, VoidCallback? onSure}) {
    return Dialog(
      backgroundColor: Colors.transparent,
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(15),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: const EdgeInsets.symmetric(vertical: 21, horizontal: 10),
              child: Text(
                title + title + title + title + title,
                textAlign: TextAlign.center,
                style: const TextStyle(color: Colors.black, fontWeight: FontWeight.w700, fontSize: 16),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 15),
              child: TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                    Container(
                      decoration: BoxDecoration(
                        color: MyColours.appMain,
                        borderRadius: BorderRadius.circular(20),
                      ),
                      padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 10),
                      alignment: Alignment.center,
                      child: const Text("知道了",
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 18,
                            fontWeight: FontWeight.w700,
                          )),
                    ),
                  ])),
            ),
            const SizedBox(height: 10),
          ],
        ),
      ),
    );
  }

  //ios的风格的弹框
  static _buildIOSModeWidgte(BuildContext context, String title, String content,
      {VoidCallback? onClose, VoidCallback? onSure, String? close, String? sure}) {
    return CupertinoTheme(
      data: const CupertinoThemeData(
        barBackgroundColor: Colors.white,
        brightness: Brightness.light,
      ),
      child: CupertinoAlertDialog(
        title: Text(
          title,
          style: const TextStyle(color: Colors.black, fontSize: 16, fontWeight: FontWeight.bold),
        ),
        content: content.isNotEmpty
            ? Text(content, style: const TextStyle(color: Colors.black, fontSize: 16, fontWeight: FontWeight.normal))
            : Container(),
        actions: [
          CupertinoDialogAction(
            child: Text(
              sure ?? "确定",
              style: const TextStyle(color: MyColours.ff0060ff, fontSize: 16),
            ),
            onPressed: () {
              Navigator.pop(context, "确定");
              if (onSure != null) {
                onSure();
              }
            },
          ),
          CupertinoDialogAction(
            child: Text(
              close ?? "取消",
              style: const TextStyle(color: MyColours.ff0060ff, fontSize: 16),
            ),
            onPressed: () {
              Navigator.pop(context, "取消");
            },
          ),
        ],
      ),
    );
  }

  //自定义内容的弹框
  static _buildCustomWidget(Widget child, {VoidCallback? onClose, VoidCallback? onSure}) {
    return Dialog(backgroundColor: Colors.transparent, surfaceTintColor: Colors.transparent, child: child);
  }

  //支付的状态
  static _buildSheetPayStatue(BuildContext context, String title, String content,
      {VoidCallback? onClose, VoidCallback? onSure, Map<String, dynamic>? object}) {
    return BottomSheet(
        onClosing: () {},
        backgroundColor: Colors.transparent,
        builder: (bContext) {
          return Container(
            padding: const EdgeInsets.symmetric(vertical: 135),
            decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(topLeft: Radius.circular(16.5), topRight: Radius.circular(16.5))),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                MyImage(
                  imageName: content, //ic_pay_success
                  width: 50.w,
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 15, bottom: 20),
                  child: Text(
                    title,
                    style: const TextStyle(color: Colors.black, fontSize: 17),
                  ),
                ),
                TextButton(
                    onPressed: () {
                      Navigator.pop(context);
                      if (onSure != null) {
                        onSure();
                      }
                    },
                    child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                      Container(
                        decoration: BoxDecoration(
                          color: MyColours.appMain,
                          borderRadius: BorderRadius.circular(20),
                        ),
                        padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 10),
                        alignment: Alignment.center,
                        child: Text(object?["buttonTitle"] ?? "查看订单",
                            style: const TextStyle(
                              color: Colors.white,
                              fontSize: 18,
                              fontWeight: FontWeight.w700,
                            )),
                      ),
                    ])),
                const SizedBox(height: kToolbarHeight),
              ],
            ),
          );
        });
  }

  //弹起自定义的弹框
  static _buildSheetCustomWidget(Widget child) {
    return BottomSheet(
        onClosing: () {},
        backgroundColor: Colors.transparent,
        builder: (bContext) {
          return child;
        });
  }
}

// // 使用封装函数的示例
// void _showAlert() {
//   showIOSAlert(
//     context,
//     title: "Alert Title",
//     content: "This is an iOS style alert message.",
//     onClose: () {
//       // 对话框关闭后的操作
//       print("Dialog closed");
//     },
//   );
// }

// ... 其余代码保持不变 ...

// // 在你的Widget的某个地方调用这个函数来显示弹框
// FloatingActionButton(
//   onPressed: _showAlert,
//   tooltip: 'Show iOS Style Alert',
//   child: Icon(Icons.add),
// ),
